//AccountBaseConvertor.java
package com.lqjava.flowable.beans.convertor;

import com.lqjava.flowable.beans.convertor.common.*;
import com.lqjava.flowable.beans.entity.domain.AccountBase;
import com.lqjava.flowable.beans.entity.request.IDRequest;
import com.lqjava.flowable.beans.entity.request.AccountBaseOptionRequest;
import com.lqjava.flowable.beans.entity.response.AccountBaseViewResponse;
import com.lqjava.flowable.beans.utils.LocalDateTimeUtils;

import java.util.*;
import java.util.stream.Collectors;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Component;

/**
* 账户基础Mapper
* 用于描述账户的基础信息
* @author lq 2021-03-21 18:01:05
*/
@Component
public class AccountBaseConvertor extends CommonConvertor{


    @Override
    public List<ProrVal> getProrValList(boolean isInsert) {
        return isInsert ? this.getProrValListByInsert() : this.getProrValListByUpdate();
    }

    private List<ProrVal> getProrValListByInsert(){
        return Arrays.asList(new ProrVal[]{
            new ProrVal("creatorid", "${userid}"),
            new ProrVal("creatorName", "${username}"),
            new ProrVal("createdDateTime", "${now}"),
            new ProrVal("modifierid", "${userid}"),
            new ProrVal("modifierName", "${username}"),
            new ProrVal("modificationDateTime", "${now}"),
            new ProrVal("enabled", "1"),
            new ProrVal("deleted", "0"),
        });
    }
    private List<ProrVal> getProrValListByUpdate(){
        return Arrays.asList(new ProrVal[]{
            new ProrVal("modifierid", "${userid}"),
            new ProrVal("modifierName", "${username}"),
            new ProrVal("modificationDateTime", "${now}"),
        });
    }

    public AccountBase convert(IDRequest request) {
        AccountBase target = new AccountBase();
        target.setId(request.getId());
        return target;
    }


    public AccountBase convert(AccountBaseOptionRequest source) {
        AccountBase target = new AccountBase();
        BeanUtil.copyProperties(source, target);
        return target;
    }

    public Map<Long,AccountBase> convert2IdMap(List<AccountBase> sourceList) {
        if(sourceList == null || sourceList.size() == 0) return new HashMap<>();
        return sourceList.stream().collect(Collectors.toMap(AccountBase::getId,e -> e));
    }

    public List<Long> convert2IdList(List<AccountBase> sourceList) {
        if(sourceList == null || sourceList.size() == 0) return new ArrayList<>();
        return sourceList.stream().map(AccountBase::getId).collect(Collectors.toList());
    }

    public AccountBaseViewResponse convert2View(AccountBase source) {
        AccountBaseViewResponse target = new AccountBaseViewResponse();
        BeanUtil.copyProperties(source, target);

        target.setCreatedDateTime(LocalDateTimeUtils.format(source.getCreatedDateTime()));
        target.setModificationDateTime(LocalDateTimeUtils.format(source.getModificationDateTime()));

        return target;
    }

    public List<AccountBaseViewResponse> convert2ViewList(List<AccountBase> sourceList) {
        List<AccountBaseViewResponse> targetList = new ArrayList<>();
        sourceList.forEach(source -> targetList.add(convert2View(source)));
        return targetList;
    }
}
